package com.loe.ui

import android.view.View
import android.widget.EditText
import com.loe.mvp.BaseView
import com.loe.mvp.ext_java.delay
import com.loe.mvp.ext_java.float
import com.loe.mvp.ext_java.safe
import com.loe.mvp.ext_view.setDelayClickListener
import com.loe.ui.util.CornerProvider
import com.loe.ui.api.IUiView
import com.loe.ui.view.UiViewPager
import com.quickjs.JSObject

fun View.initBg(ui: JSObject)
{
    ui.gotHas("bg")
    {
        if (it is JSObject)
        {
            background = it.shape
        } else
        {
            this.bg = it.toString()
        }
    }
}

fun View.initPadding(ui: JSObject)
{
    val padding = ui.getPadding()
    setPadding(padding[0], padding[1], padding[2], padding[3])
}

fun View.initScale(ui: JSObject)
{
    ui.gotHasString("scale")
    {
        val scale = it.split(",")
        if (scale.size > 1)
        {
            scaleX = scale[0].float
            scaleY = scale[1].float
        } else
        {
            val scaleFloat = scale[0].float
            scaleX = scaleFloat
            scaleY = scaleFloat
        }
    }
}

fun View.initOnClick(ui: JSObject)
{
    ui.gotHasFunction("onClick")
    { callback ->
        this as IUiView
        setOnClickListener { safe { callback.call(jsObject, null) } }
    }
}

fun View.initDelayClick(ui: JSObject)
{
    ui.gotHasFunction("delayClick")
    { callback ->
        this as IUiView
        setDelayClickListener { safe { callback.call(jsObject, null) } }
    }
}

fun View.initLink(ui: JSObject)
{
    ui.gotHasString("link")
    { link ->
        setOnClickListener { safe { (this.context as BaseView).openUrl(link) } }
    }
}

fun View.initFocus(ui: JSObject)
{
    ui.gotHasBoolean("focus")
    { focus ->
        isFocusableInTouchMode = focus
        isFocusable = focus
        if (focus && this is EditText)
        {
            clearFocus()
            requestFocus()
            setSelection(text.length)
        }
    }
}

fun View.initSelector(ui: JSObject)
{
    ui.gotHasObject("selector")
    { selector ->
        if (this is UiViewPager)
        {

        } else
        {
            this as IUiView
            selector.executeFunction2("add", jsObject)
            setOnClickListener()
            {
                selector.executeFunction2("select", jsObject)
                onSelect?.invoke()
            }
            // isSelect
            if (ui.gotBoolean("isSelect"))
            {
                delay()
                {
                    selector.executeFunction2("select", jsObject)
                    onSelect?.invoke()
                }
            }
        }
    }
}

fun View.initOnSelect(ui: JSObject)
{
    ui.gotHasFunction("onSelect")
    { callback ->
        this as IUiView
        onSelect = { safe { callback.call(jsObject, null) } }
    }
}

fun View.initCorner(ui: JSObject)
{
    ui.gotHasString("corner")
    {
        val corner = it.px
        val cornerDirect = ui.gotString("cornerDirect")
        outlineProvider = CornerProvider(corner, cornerDirect, this)
    }
}